package com.operator.Activitys.adapter.base;

/**
 * 项目名称：
 * 类描述：公用 BaseRecyclerView Adapter
 * 创建人：张飞祥
 * 创建时间：2017/9/18 下午2:17
 * 修改人：张飞祥
 * 修改时间：2017/9/18 下午2:17
 * 修改备注：
 */

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    public  List<T> myList;
    public  static Context mContext;
    public  static LayoutInflater mInflater;

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public BaseRecyclerViewAdapter(Context context, List<T> list) {

        if (context != null){
            mContext = context;
            mInflater = LayoutInflater.from(context);
            myList = list;
        }
    }

    //Item布局
    protected abstract int getLayoutItemResId();

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (getLayoutItemResId() != 0){
            View itemView = mInflater.inflate(getLayoutItemResId(), parent, false);
            itemView.setOnClickListener(this);
            return getViewHolder(itemView);
        }
        return null;
    }

    public abstract void onBindViewHolder(RecyclerView.ViewHolder holder, int position);

    @Override
    public int getItemCount() {
        //RecyclerView的count设置为数据总条数
        return myList.size();
    }

    //Adapter中定义接口,模拟ListView的OnItemClickListener：
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);
    }
    //将点击事件转移给外面的调用者：
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v, (Integer) v.getTag());
        }
    }
    //最后暴露给外面的调用者，定义一个设置Listener的方法（）：
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    /**
     * 添加数据到底部
     * @param items
     */
    public void addFooterItem(List<T> items) {
        myList.addAll(items);
        notifyDataSetChanged();
    }


    /**
     * 删除某一个
     * @param position
     */
    public void removeItem(int position) {
        myList.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(0,myList.size());
    }

    /**
     * 清空全部数据
     */
    public void clearDataAll() {
        myList.clear();
        notifyDataSetChanged();
    }

    /**
     *
     * 动态传入ViewHolder
     *
     */
    public abstract RecyclerView.ViewHolder getViewHolder(View view);
}